package com.neutech.util;

import com.neutech.constant.Constant;
import com.sun.org.apache.regexp.internal.RE;

import java.sql.*;

public class JdbcUtil {
    public static void main(String[] args) {
        System.out.println(getConnection());
    }
    public static Connection getConnection() {
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(Constant.URL, Constant.USER, Constant.PASSWORD);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static Statement getStatement(Connection connection) {
        Statement statement = null;
        try {
            statement = connection.createStatement();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return statement;
    }
    //封装PreparedStatement
    public static PreparedStatement getPreparedStatement(Connection connection,String sql){
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return preparedStatement;
    }


    public static int executeUpdate(Statement statement, String sql) {
        ResultSet resultSet = null;
        int rows = 0;
        try {
            rows = statement.executeUpdate(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return rows;
    }


    public static ResultSet getResultSet(Statement statement, String sql) {
        ResultSet resultSet = null;
        try {
            resultSet = statement.executeQuery(sql);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return resultSet;
    }
    public static ResultSet getResultSet(PreparedStatement preparedStatement){
        ResultSet resultSet = null;
        try {
            resultSet = preparedStatement.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return resultSet;
    }
    public static void rollback(Connection connection){
        try {
            connection.rollback();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public static void close(Statement statement, Connection connection) {

        try {
            if (statement != null && connection != null) {
                statement.close();
                connection.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public static void close(ResultSet resultSet,PreparedStatement preparedStatement,Connection connection){
        try {
            resultSet.close();
            preparedStatement.close();
            connection.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public static void close(Statement statement, Connection connection, ResultSet resultSet) {

        try {
            if (resultSet != null && statement != null && connection != null) {
                statement.close();
                connection.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }

}

